home *** CD-ROM | disk | FTP | other *** search
- unit Abd_main;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- IniFiles, Forms, Dialogs, StdCtrls, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- Label1: TLabel;
- Image1: TImage;
- Bevel1: TBevel;
- procedure FormActivate(Sender: TObject);
- private
- { Private declarations }
- Procedure LaunchDiary;
- Function SetupNetIdapi( const NetProg : string ) : boolean;
- Function SetupLocalIdapi( const LocalIdapiProg : string ) : boolean;
- Procedure SetWinIdapi( szIdapi : string );
- Procedure RunDiary( const LocalFName : string );
- Function NewerVersionFound( const LocalFName, NetFName : string ) : boolean;
- Function CopyNetworkProgram( const LocalFName, NetFName : string ) : boolean;
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- type
- PBOOLEAN = ^boolean;
-
- function EnumFunc( Wnd:HWND; pRunning : PBOOLEAN ): bool; export;
-
- implementation
-
- {$R *.DFM}
-
- function EnumFunc(Wnd:HWND; pRunning : PBOOLEAN ): bool;
- var
- ClassName : array[0..30] of char;
- begin
- Result := true;
- GetClassName( Wnd, ClassName, 29 );
- if StrIComp( ClassName, 'TF_ABMAIN' ) = 0 then begin
- pRunning^ := TRUE;
- Result := false;
- end;
- end;
-
- Function CheckWindows : boolean;
- var
- bAlreadyRunning : boolean;
- begin
- bAlreadyRunning := FALSE;
- EnumWindows( @EnumFunc, longint( @bAlreadyRunning ) );
- Result := not bAlreadyRunning;
- end;
-
- procedure TForm1.FormActivate(Sender: TObject);
- begin
- if CheckWindows then
- LaunchDiary
- else begin
- ShowMessage( 'Diary Program already running' );
- Close;
- end;
- end;
-
- Procedure TForm1.SetWinIdapi( szIdapi : string );
- var
- Ini : TInifile;
- szWindows : array[ 0..255 ] of char;
- FName : string;
- begin {SetWinIdapi}
- GetWindowsDirectory( szWindows, sizeof( szWindows ) -1 );
- FName := StrPas( szWindows ) + '\win.ini';
- Ini := TIniFile.Create( FName );
- try
- Ini.WriteString( 'IDAPI', 'CONFIGFILE01', szIdapi );
- finally
- Ini.Free;
- end;
- end;
-
- Function TForm1.SetupNetIdapi( const netProg : string ) : boolean;
- var
- szNetIdapi : string;
- begin {SetupNetIdapi}
- Result := FALSE;
- szNetIdapi := ExtractFilePath( NetProg ) + 'idapi.cfg';
- if fileexists( szNetIdapi ) then begin
- SetWinIdapi( szNetIdapi );
- Result := TRUE;
- end else
- showmessage( 'Error - Network IDAPI file not found' );
- end;
-
- Function TForm1.SetupLocalIdapi( const LocalIdapiProg : string ) : boolean;
- begin {SetupLocalIdapi}
- Result := FALSE;
- if fileexists( LocalIdapiProg ) then begin
- SetWinIdapi( LocalIdapiProg );
- Result := TRUE;
- end else
- showmessage( 'Error - Local IDAPI file not found' );
- end;
-
- Procedure TForm1.RunDiary( const LocalFName : string );
- var
- sz0 : array[ 0..255 ] of char;
- res : integer;
- begin
- Res := Winexec( StrPCopy( sz0, LocalFName ), SW_SHOWNORMAL );
- if res < 32 then
- ShowMessage( 'Error ' + IntToStr( Res ) + ' running Diary Program' );
- end;
-
- Function TForm1.NewerVersionFound( const LocalFName, NetFName : string ) : boolean;
- {Function returns TRUE if Network file is newer than local, or if
- local file is not found, FALSE if the local file is more recent.
- NOTE: the Network file has already been found to exist}
- begin {NewerVersionFound}
- if FileExists( LocalFName ) {abdiary.exe on local} then
- Result := FileAge( LocalFName ) <= FileAge( NetFName )
- else
- Result := TRUE;
- end;
-
- Function TForm1.CopyNetworkProgram( const LocalFName, NetFName : string ) : boolean;
- const
- BUFSIZE = 60000;
- var
- fin, Fout : File;
- pBuf : pchar;
- iRead : word;
- begin {CopyNetworkProgram}
- Result := FALSE;
- {$I+}
- try
- GetMem( pBuf, BUFSIZE );
- except
- ShowMessage( 'Insufficient Memory to copy New Diary Program' );
- exit;
- end;
-
- try
- AssignFile( fin, NetFName );
- AssignFile( fout, LocalFName );
- try
- filemode := fmOpenRead + fmShareDenyWrite;
- reset( fin, 1 );
- try
- filemode := fmOpenWrite + fmShareExclusive;
- rewrite( fout, 1 );
- except
- ShowMessage( 'Unable to update Local Diary Program' );
- exit;
- end;
-
- while not eof( fin ) do begin
- blockread( fin, pBuf^, BUFSIZE , iread );
- blockwrite( fout, pBuf^, iRead );
- end;
- CloseFile( fin );
- CloseFile( fout );
- result := TRUE;
- ShowMessage( 'New Version of Diary Program copied from network' );
- except
- ShowMessage( 'Error copying Diary Program' );
- end;
-
- finally
- Freemem( pBuf, BUFSIZE );
- end;
- end;
-
- Procedure TForm1.LaunchDiary;
- var
- LocalFName, NetFName, LocalIdapiFName : string;
- Ini : TIniFile;
- i, i1 : integer;
- td : tDateTime;
- bLaunch : boolean;
- const
- Second : extended = 1.0 / 24 / 60 / 60;
- begin {LaunchDiary}
- td := Now;
- repeat
- Application.ProcessMessages;
- until ( Now - td ) > Second;
-
- Ini := TIniFile.Create( ExtractFilePath( Application.ExeName ) +
- 'ABDIARY.INI' );
- try
- with Ini do begin
- LocalFName := ReadString( 'DIR', 'LOCAL',
- ExtractFilePath( Application.ExeName ) );
- if LocalFName[ length( LocalFName ) ] <> '\' then
- LocalFName := LocalFName + '\';
- LocalFName := LocalFName + 'abdiary.exe';
-
- NetFName := ReadString( 'DIR', 'NET', 'J:\' );
- if NetFName[ length( NetFName ) ] <> '\' then
- NetFName := NetFName + '\';
- NetFName := NetFName + 'abdiary.exe';
-
- LocalIDapiFName := ReadString( 'DIR', 'LOCALIDAPI', 'C:\IDAPI' );
- if LocalIdapiFName[ length( LocalIdapiFName ) ] <> '\' then
- LocalIdapiFName := LocalIdapiFName + '\';
- LocalIdapiFName := LocalIdapiFName + 'idapi.cfg';
- end;
-
- finally
- Ini.Free;
- end;
-
- bLaunch := TRUE;
- if FileExists( NetFName ) then begin {we are networked}
-
- if NewerVersionFound( LocalFName, NetFName ) then
- bLaunch := CopyNetworkProgram( LocalFName, NetFName );
-
- if bLaunch then
- bLaunch := SetupNetIdapi( NetFName );
-
- end else if FileExists( LocalFName ) then {we are local}
-
- bLaunch := SetupLocalIdapi( LocalIdapiFName )
-
- else begin {we are undone!}
-
- ShowMessage( 'Diary Program not found (neither Local nor Networked)' );
- bLaunch := FALSE;
-
- end;
-
- if bLaunch then
- RunDiary( LocalFname );
-
- Close; {i.e. terminate application}
-
- end;
-
- end.
-